onDropContent
onDropContent is a view modifier provided by Scripting that allows a view to act as a drop target, receiving files, images, or text dragged in from other applications.
Overview
With onDropContent, you can:
- Receive drag-and-drop content from other apps
- Restrict acceptable content using UTType identifiers
- Track whether a drag operation is hovering over the view
- Start loading dropped content through
ItemProvider - Establish persistent access to security-scoped files when needed
Modifier Definition
Parameters
types
Specifies the list of content types that the view can accept, expressed as UTType strings.
If the drag operation does not contain any of the specified types:
- The view does not activate as a drop target
isTargetdoes not updateperformis not called
Example:
isTarget
Indicates whether the drag operation is currently hovering over the view.
- The value is
truewhen the drag enters the view’s area - The value is
falsewhen the drag exits the area
Two forms are supported:
-
Binding object form
-
Observable form
The observable form works well with useObservable and provides a more concise reactive binding.
perform
Called when content matching the specified types is dropped onto the view.
attachmentsis an array ofItemProvider- Each
ItemProviderrepresents one dropped item - The return value indicates whether the drop was successfully handled
Return value semantics:
- Return
trueto indicate the drop was accepted - Return
falseto indicate the drop was not handled
Execution Rules for perform
The following rules must be followed inside perform:
- Loading of
ItemProvidercontents must be started synchronously within the execution scope ofperform - Asynchronous completion is allowed using
Promiseorthen - Loading must not be initiated later from a different callback or event
- If
performreturnsfalse, the system treats the drop as unhandled
Reasoning:
- Dropped content is protected by system security rules
- Access to the dropped payload is only valid while
performis executing - If loading does not begin within this scope, the content may no longer be accessible
Working with ItemProvider
Within perform, you should inspect each ItemProvider and start loading based on its capabilities.
Typical steps include:
- Checking type conformance using
hasItemConforming - Selecting an appropriate loading method
- Handling files, images, or text accordingly
Example Usage
Security-Scoped File Access
File paths obtained via onDropContent are typically security-scoped resources.
These paths may become invalid when:
performreturns- The app restarts
- The script lifecycle ends
To retain long-term access, you should create a file bookmark as soon as the path is obtained.
FileManager.addFileBookmark
Description:
- Creates a security-scoped bookmark for a file or folder
- Intended for paths obtained via APIs such as
PhotosoronDropContent - Returns the bookmark name, or
nullif creation fails
Example:
FileManager.removeFileBookmark
Description:
- Removes a previously created file bookmark
- Should be called when access to the file is no longer needed
- Returns whether the removal was successful
Example:
Usage Recommendations
- Specify
typesas precisely as possible - Use
performonly to start loading, not to wait for results - Load images and lightweight data as objects when appropriate
- Prefer file paths for large resources such as videos or documents
- Create bookmarks for files that require long-term access
- Remove bookmarks when the associated files are no longer needed
